﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Framework
{
    public abstract class View : ViewLoader
    {
        public IModule BelongModule { get; private set; }

        protected List<int> timerIds;

        public virtual void PreInit(IModule belongModule, IEventDispatcher eventDispatcher, ILoader resLoader, IModuleEventDispatcher moduleEventDispatcher)
        {
            BelongModule = belongModule;

            SetEventDispatcher(eventDispatcher, moduleEventDispatcher);
            
            _resLoader = resLoader;

            modulePath = "Bundles/Module/" + belongModule.moduleName + "/";
            
            Log.Debug("View PreInit");

            Init();
            RegisterEvent();
        }

        /// <summary>
        /// 倒计时
        /// </summary>
        /// <param name="interval">间隔时间</param>
        /// <param name="stepCallback">每步回调</param>
        /// <param name="finishCallback">结束回调</param>
        /// <param name="finishTime">结束时刻</param>
        /// <returns>Timer Id</returns>
        public int CountDown(float interval, Action<float> stepCallback, Action finishCallback,
            long finishTime = long.MaxValue)
        {
            int id = Timer.Instance.CountDown(interval, stepCallback, finishCallback, finishTime);
            if (timerIds == null)
            {
                timerIds = new List<int>();
            }
            timerIds.Add(id);
            return id;
        }
        
        /// <summary>
        /// 延迟调用
        /// </summary>
        /// <param name="delay">延迟时间</param>
        /// <param name="callback">调用方法</param>
        /// <returns>Timer Id</returns>
        public int DelayCall(float delay, Action callback)
        {
            int id = Timer.Instance.DelayCall(delay, callback);
            if (timerIds == null)
            {
                timerIds = new List<int>();
            }
            timerIds.Add(id);
            return id;
        }

        /// <summary>
        /// 初始化UI,关联Unity组件
        /// </summary>
        protected abstract void Init();

        /// <summary>
        /// 统一注册事件（UGUI事件）
        /// </summary>
        protected virtual void RegisterEvent() { }

        /// <summary>
        /// 在模块根节点下加载View，和firstView同级
        /// </summary>
        /// <param name="path"></param>
        /// <param name="view">要绑定的脚本</param>
        public View AddModuleView(string path, Type view)
        {
            GameObject go = LoadGameObject(modulePath+path, true);
            View component = go.AddScriptComponent(view) as View;
            BelongModule.AddView(component);
            return component;
        }

        public T AddChildView<T>(string path, int siblingIndex = -1) where T:View
        {
            GameObject go = LoadGameObject(path, true);
            View component = (View)go.AddScriptComponent(typeof(T));
            BelongModule.ConnectView(component);
            component.transform.SetParent(transform, false);
            if(siblingIndex >= 0)
            {
                component.transform.SetSiblingIndex(siblingIndex);
            }
            return (T)component;
        }
        
        public T ConnectView<T>(Transform trans) where T:View
        {
            View component = trans.gameObject.AddScriptComponent(typeof(T)) as View;
            BelongModule.ConnectView(component);
            return (T)component;
        }

        private void OnDestroy()
        {
            //TODO:View Destroy
            Log.Warning("View OnDestroy");
        }

        public override void Dispose()
        {
            base.Dispose();
            if(timerIds != null)
            {
                Timer.Instance.RemoveTimers(timerIds);
            }
        }
    }
}